<template>
    <div>
        <el-page-header @back="gotoback" title="返回">
            <template #content>
                <el-breadcrumb-item :to="{ path: '/organization' }">新增·组织</el-breadcrumb-item>
            </template>
        </el-page-header>
    </div>
    <div class="header">
        <el-row>
            <el-col :span="12">
                <div class="back-ground wich">
                    <span class="span">名称</span>
                </div>
            </el-col>
            <el-col :span="12">
                <div class="back-ground wich">
                    <span class="span">序号</span>
                </div>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <div class="back-ground">
                    <el-input v-model="name" placeholder="在此输入用户名" clearable class="input" />
                </div>
            </el-col>
            <el-col :span="12">
                <div class="back-ground">
                    <el-input-number v-model="num" :min="1" :max="10" @change="handleChange" class="input" />
                </div>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <div class="back-ground wich">
                    <span class="span">职务(代表只有该职务员工才能成为负责人)</span>
                </div>
            </el-col>
            <el-col :span="12">
                <div class="back-ground wich">
                    <span class="span">等级</span>
                </div>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <div class="back-ground">
                    <el-select v-model="regions" @change="change" multiple filterable allow-create default-first-option
                        placeholder="负责人职务,可以选择多个" class="input">
                        <el-option v-for="(item) in region" :key="item.id" :label="item.name" :value="item.id"
                            :disabled="disabled">
                        </el-option>
                    </el-select>
                </div>
            </el-col>
            <el-col :span="12">
                <div class="back-ground">
                    <el-input v-model="lv" class="input" disabled placeholder="0
                                                " />
                </div>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <div class="back-ground wich">
                    <span class="span">图标</span>
                </div>
            </el-col>
            <el-col :span="12">
                <div class="back-ground wich">
                    <span class="span">上级组织</span>
                </div>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <div class="back-ground">
                    <el-input v-model="icon" clearable style="margin-top: 1vh;" />
                </div>
            </el-col>
            <el-col :span="12">
                <div class="back-ground">
                    <el-tag class="tag">

                    </el-tag>
                </div>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <div class="back-ground wich">
                    <span class="span">描述</span>
                </div>
            </el-col>
            <el-col :span="12">
                <div class="back-ground wich">
                    <span class="span">操作</span>
                </div>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
                <div class="back-ground">
                    <el-input v-model="explain" clearable style="margin-top: 1vh;" />
                </div>
            </el-col>
            <el-col :span="12">
                <div class="back-ground">
                    <el-button type="primary" class="tag" @click="add">新增</el-button>
                </div>
            </el-col>
        </el-row>
    </div>
</template>
<script lang="ts">
import { defineComponent, reactive, toRefs, onMounted } from 'vue'
import { AddOrganization, GetZhiwuList } from '../../../api/user/user'
// import { ElMessageBox } from 'element-plus';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router';

export default defineComponent({
    setup() {
        const router = useRouter();

        const data = reactive({
            disabled: false,//禁用
            num: 1,//计数器
            name: '',//姓名
            value: '',//下拉框
            lv: 0,//禁用框,等级
            icon: '',//图标
            explain: '',//描述
            region: [],//下拉框
            regions: [],
            id: ''
        })
        const events = {
            gotoback() {
                window.history.go(-1)
            },
            //计数器
            handleChange() {

            },
            //查询下拉框绑值
            datas() {
                GetZhiwuList({
                    limit: "20"
                }).then((res: any) => {
                    console.log('下拉框数据---', res)
                    data.region = res.data.data
                })
            },
            //下拉框选中值发生变化时触发的漫画及
            change(val: any) {
                if (val.length >= 5) {
                    console.log('qlw', val.length)
                    ElMessage({
                        message: '不能超过五个职位！！！',
                        type: 'info',
                    })
                    data.disabled = true
                } else {
                    data.disabled = false
                }

                let aa = []
                console.log('打印职务----', val)

                val.forEach((res: any) => {
                    data.region.forEach((na: any) => {
                        if (res == na.id) {
                            aa.push(na)
                            data.id = aa
                        }
                    })
                })


                if (aa.length == 0) {
                    data.lv = 0
                } else {
                    data.lv = Math.max.apply(Math, aa.map(function (o) {
                        return o.lv
                    }))
                }

            },
            //新增
            add() {
                let dd = []
                data.id.forEach((res: any) => {
                    dd.push(res.id)
                })
                AddOrganization({
                    name: data.name,
                    no: data.num,
                    lv: data.lv,
                    icon: data.icon,
                    explain: data.explain,
                    parent: null,
                    zids:dd
                }).then((res: any) => {
                    console.log('新增组织---', res)
                    if (res.result == 0) {
                        ElMessage({
                            message: '添加成功',
                            type: 'success',
                        })
                        router.push("/organization")
                    } else {
                        ElMessage({
                            message: '添加失败',
                            type: 'error',
                        })
                    }
                })
            }
        }
        onMounted(() => {
            events.datas()
        })
        return {
            ...events,
            ...toRefs(data)
        }
    },
})
</script>
<style scoped>
.header {
    border: #ccc solid 1px;
    margin-top: 3vh;
    border-radius: 5px;
}

.back-ground {
    border: #efebeb solid 1px;
    height: 50px;
}

.span {
    float: left;
    margin-top: 2vh;
    margin-left: 1vh;
    color: #c4c7d0;
}

.wich {
    background-color: rgba(240, 239, 245, 0.479);
}

.input {
    margin-top: 1vh;
    width: 70vh
}

.tag {
    float: left;
    margin-top: 2vh;
    margin-left: 5vh;
}
</style>